import { ElMessage } from 'element-plus'
import { productCategoty, productQuery } from '@/network/model/cartoonModel'
export class CartoonCategory {
  api: any;
  constructor(api: any) {
    this.api = api
  }
  // 获取分类列表
  async getProductList(params: productQuery, deliveryName: string) {
    try {
      const res = await this.api[deliveryName](params)
      if (res?.code !== 200) {
        // ElMessage.error(res.msg)
        return resBody(201, res.data)
      }
      return resBody(200, res.data)

    } catch (error) {
      console.log(error);
      // ElMessage.error('网络接口错误')
      return resBody(201, error)
    }

  }

  // 添加分类
  async addCartoon(params: productCategoty, deliveryName: string) {
    if (!params.name) return ElMessage.error('分类名称不能为空')
    try {
      const { code, msg, data } = await this.api[deliveryName](params)
      if (code !== 200) {
        ElMessage.error(msg)
        return resBody(201, data)
      }
      ElMessage.success(msg)
      return resBody(200, data)
    } catch (error) {
      ElMessage.error('网络接口错误')
      return resBody(201, error)

    }
  }
  // 修改分类
  async editCartoon(params: productCategoty, deliveryName: string) {
    if (!params.name) return ElMessage.error('分类名称不能为空')
    try {
      const { code, msg, data } = await this.api[deliveryName](params)
      if (code !== 200) {
        ElMessage.error(msg)
        return resBody(201, data)
      }
      ElMessage.success(msg)
      return resBody(200, data)
    } catch (error) {
      ElMessage.error('网络接口错误')
      return resBody(201, error)

    }
  }
  // 根据id删除
  async deleteItem(id: Number, deliveryName: string) {
    try {
      const { code, msg } = await this.api[deliveryName]({ ids: id.toString() })
      if (code !== 200) {
        ElMessage.error(msg)
        return resBody(201)
      }
      ElMessage.success(msg)
      return resBody()
    } catch (error) {
      ElMessage.error('网络接口错误')
      return resBody(201, error)
    }
  }
}

function resBody(code: number = 200, data: any = {}) {
  return { code, data }
}